/*
* tedi2ad library code
* Copyright (C) <2022>  <alkeon> [alkeon@autistici.org]
   
* Texdi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Texdi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tedi2ad.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include "../tedi2lang/tedi2lang.h"
#include "tedi2ad.h"
#include "../tedi2lang/exception.h"

using namespace std;

#define BLOCK 2
#define IMAGE 1
#define LINK 0
#define NO_TAG -1

tedi2ad::tedi2ad(tags_definition td): tedi2lang(td){}

tedi2ad::tedi2ad(): tedi2lang(default_tags_value()){}

tags_definition tedi2ad::default_tags_value(){
	
	tags_definition td;

	td.start_heading_first_level_tag = "= ";
	td.start_heading_second_level_tag = "== ";
	td.start_heading_third_level_tag = "=== ";
	td.start_heading_fourth_level_tag = "==== ";

	td.start_list_tag = "";
	td.list_item_tag = "* ";
	td.end_list_tag = "";

	td.start_container_tag = "";
	td.middle_container_tag = "";
	td.end_container_tag = "";

	td.start_link_tag = "";
	td.middle_link_tag = "[";
	td.end_link_tag = "]";

	td.start_image_tag = "image::";
	td.middle_image_tag = "[";
	td.end_image_tag = "]";

	td.start_table_tag = "|=======";
	td.end_table_tag = "|=======";
	td.start_table_row_tag = "";
	td.end_table_row_tag = "";
	td.start_table_data_tag = "|";
	td.end_table_data_tag = "";

	td.end_paragraph_tag = "";

	return td;
}

/*
 * Deletes heading tag and insert image tag
 * - ([caption] image.png)
 * - image::image.png[caption]
 *
 */
void tedi2ad::convert_line_image(string& line) {

	size_t start_tag = get_not_escaped_tag(line, "([");
	if(found(start_tag) && !is_tag_escaped(line, start_tag)) {

		line = line.erase(start_tag, 2);

		size_t square_bracket = get_not_escaped_tag(line, "] ");
		if(found(square_bracket) && !is_tag_escaped(line, square_bracket)) {

			string caption = line.substr(start_tag, square_bracket - start_tag);

			line = line.erase(start_tag, square_bracket + 2 - start_tag);
			line = line.insert(start_tag, _start_image_tag);

			size_t last_parenthesis = correct_position(line, start_tag + _start_image_tag.size(), '(', ')');
			if(found(last_parenthesis)) {
				line = line.erase(last_parenthesis, 1);
				line = line.insert(last_parenthesis, "[" + caption + "]");
			} else
				throw Invalid("Missing ')' in images tag.",line);
		} else
			throw Invalid("Missing ']' in images tag.",line);
	} else
		throw Invalid("Missing images tag.",line);

}

/*
 * Deletes block tag and insert block tag
 *
 */
void tedi2ad::convert_line_block(string& line) {

	size_t start_tag = get_not_escaped_tag(line, "{(");
	if(found(start_tag) && !is_tag_escaped(line, start_tag)) {

		line = line.erase(start_tag, 2);
		line = line.insert(start_tag, _start_container_tag);

		size_t parenthesis = get_not_escaped_tag(line, ") ");
		if(found(parenthesis) && !is_tag_escaped(line, parenthesis)) {
			line = line.erase(start_tag, parenthesis - start_tag + 2);

			size_t end_tag = correct_position(line, start_tag, '{', '}');
			if(found(end_tag))
				line = line.erase(end_tag, 1);
			else
				++_open_brackets;
		} else
			throw Invalid("Missing ')' in block tag.",line);
	} else
		throw Invalid("Missing block tag.",line);

	_has_block = true;
}
